﻿using System.Web.Mvc;
using DNT.RavenQA.Web.Infrastructure.Filters;
using DNT.RavenQA.Web.Infrastructure.IoC;
using DNT.RavenQA.Web.Infrastructure.Security;
using log4net;
using Raven.Client;

namespace DNT.RavenQA.Web.Infrastructure.Registry
{
    public class ApplicationRegistry : StructureMap.Configuration.DSL.Registry
    {
        private static readonly ILog Log = LogManager.GetLogger("DNT.RavenQA.Web");

        public ApplicationRegistry()
        {
            For<ILog>().Singleton().Use(Log);
            For<IControllerActivator>().Use<StructureMapControllerActivator>();
            For<IDocumentStore>().Singleton()
                .Use(DocumentStoreFactory.Build);
            For<IDocumentSession>().HttpContextScoped()
                .Use(x => x.GetInstance<IDocumentStore>().OpenSession());
            For<IOpenIdAuthenticationService>().HttpContextScoped()
                .Use<OpenIdAuthenticationService>();

            For<IActionInvoker>().Use<InjectingActionInvoker>();

            ForConcreteType<FirstRunCheckAttribute>().Configure
                .Setter(i => i.Session)
                .IsTheDefault();

            ForConcreteType<UnitOfWorkAttribute>().Configure
                .Setter(i => i.Session)
                .IsTheDefault();

            ForConcreteType<UserInjectorGlobalFilter>().Configure
                .Setter(i => i.Session)
                .IsTheDefault();
        }
    }
}